//
//  BSADViewController.m
//  baisibudejie
//
//  Created by 陈博文 on 15/12/18.
//  Copyright © 2015年 陈博文. All rights reserved.
//

#import "BSADViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension.h>
#import <UIImageView+WebCache.h>
#import "BSTabBarController.h"
#import "BSADModel.h"
#define BSCode @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"


@interface BSADViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *adImageVIew;
/** adItem*/
@property (nonatomic ,strong) BSADModel *adItem;

/** 使用代码生成 imageview*/
@property (nonatomic ,weak) UIImageView *adImageV;
@property (weak, nonatomic) IBOutlet UIButton *adButon;

@property (nonatomic ,weak) NSTimer *timer;

@property (nonatomic ,weak) AFHTTPSessionManager *mgr;

@end

@implementation BSADViewController


- (IBAction)buttonClick:(id)sender {
    
    // 销毁定时器
    [_timer invalidate];
    
    // 创建主框架界面
    BSTabBarController *tabBarVc = [[BSTabBarController alloc] init];
    
    // 销毁当前界面,进入主框架界面,modal,push,直接修改窗口的根控制器
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVc;
    
}


//使用网络的基本步骤

//创建管理者
//创建请求参数
//发送请求

- (void)viewDidLoad {
    [super viewDidLoad];
//1.设置占位数据

    //2.加载数据
    [self setUpConnectRequest];
    // 3.定时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];

    
}

-(UIImageView *)adImageV{
    if (_adImageV == nil) {
        UIImageView *adImageV = [[UIImageView alloc]init];
        _adImageV = adImageV;
        [self.view insertSubview:_adImageV belowSubview:self.adButon];
        
        adImageV.userInteractionEnabled = YES;
        // 添加点按手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jump)];
        [_adImageV addGestureRecognizer:tap];
    }
    return _adImageV;
}
- (void)jump
{
    NSURL *url = [NSURL URLWithString:_adItem.ori_curl];
    
    // 判断下是否能打开
    
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        
        // 使用safari打开网页
        [[UIApplication sharedApplication] openURL:url];
    }
    
}


-(void)setUpConnectRequest{
    // 加载广告数据,服务器数据 -> 跟服务器打交道 -> 接口文档
    
    // AFN -> cocodpods:管理第三方框架 -> podfile:加载指定的框架
    
    // 1.创建会话管理者
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    // 2.创建请求参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"code2"] = BSCode;
    
    // 3.发送请求Get
    [mgr GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功就会调用
        
        [responseObject writeToFile:@"/Users/chenbowen/Desktop/ad.plist" atomically:YES];
        
//        NSLog(@"%@",responseObject);

//        NSLog(@"%@",[responseObject class]);
        //使用一个字典接收数据
        NSDictionary *adDict = [responseObject[@"ad"] lastObject];
        
        // 字典转模型
        _adItem = [BSADModel mj_objectWithKeyValues:adDict];
        
        // 确定广告界面尺寸,广告界面图片,点击广告界面跳转到的界面
        
        CGFloat w = BSScreenW;
        
        CGFloat h = BSScreenH;
        if (_adItem.h) {
            h = BSScreenW / _adItem.w * _adItem.h;
            if (h > BSScreenH) {
                h = BSScreenH;
            }
        }
        self.adImageV.frame = CGRectMake(0, 0, w, h);

        
        // 加载图片
        [self.adImageV sd_setImageWithURL:[NSURL URLWithString:_adItem.w_picurl]];
        
        //设置占位 图片
        
//        self.adImageV
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // http过程:
        // AFN不支持 text/html,让解析响应的时候支持.
        
        // unacceptable content-type: text/html
        
        NSLog(@"%@",error);
        // 请求失败就会调用
    }];

}

// 每隔一秒就会调用
- (void)timeChange
{
    
    static int time = 3;
    
    if (time == -1) {
        // 点击跳过的事情
        [self buttonClick:nil];
        return;
    }
    
    NSString *timeStr = [NSString stringWithFormat:@"跳过(%d)",time];
    
    [_adButon setTitle:timeStr forState:UIControlStateNormal];
    
    // 更新按钮文字
    time--;
}


@end
